package main.java.design.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *     将有关状态存入具体观察者对象；在具体主题内部状态改变时，给所有登记过的观察者发出通知。
 * </p>
 *
 * @author : wangdejian
 * @version 1.0
 * @date : 2020/6/21 10:02
 */
public class LuoZhiXiang implements Star {

    private List<Fan> fanList = new ArrayList<>();
    private String starName;

    public LuoZhiXiang(String starName) {
        this.starName = starName;
    }

    @Override
    public void addFan(Fan fan) {
        fanList.add(fan);
    }

    @Override
    public void deleteFan(Fan fan) {
        fanList.remove(fan);
    }

    /**
     * 在具体主题内部状态改变时，给所有登记过的观察者发出通知.
     * @param msg
     */
    @Override
    public void notifyFans(String msg) {
        fanList.forEach(x -> x.update(msg, starName));
    }

}
